﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;

namespace He.Web.manage.logistics
{
    public partial class progress_edit : Manage
    {
        public string action = Enums.ActionEnum.Add.ToString(); //操作类型
        public int id;
        public string pos_mark;
        public string orderNumber;
        protected void Page_Load(object sender, EventArgs e)
        {
            pos_mark = GetString("pos_mark");
            string _action = GetString("action");
            orderNumber = GetString("orderNumber");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
            }

            if (!IsPostBack)
            {
                if (action == Enums.ActionEnum.Edit.ToString())
                {
                    chkLoginLevel("edit" + pos_mark);

                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("add" + pos_mark);
                }
            }
        }
        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Model.logistics_status model = new BLL.logistics_status().GetModel(_id);
            txtProgress.Text = model.progress;
            txtAddTime.Text = model.add_time;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.logistics_status bll = new BLL.logistics_status();
            Model.logistics_status model = new Model.logistics_status();

            model.order_number = orderNumber;
            model.progress = txtProgress.Text.Trim();
            model.add_time = txtAddTime.Text.Trim();
            if (bll.Add(model) > 0)
            {
                SaveLogs(string.Format("[物流管理]增加物流单号:{0}", model.order_number));
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.logistics_status bll = new BLL.logistics_status();
            Model.logistics_status model = new BLL.logistics_status().GetModel(_id);

            model.progress = txtProgress.Text.Trim();
            model.add_time = txtAddTime.Text.Trim();

            if (bll.Update(model))
            {
                SaveLogs(string.Format("[物流管理]修改物流单号:{0}", model.order_number));
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string url = CookieHelper.Get("LastListUrl");
            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (string.IsNullOrEmpty(url))
                {
                    url = Request.RawUrl;
                }

                if (!DoEdit(this.id))
                {
                    JscriptPrint("保存过程中发生错误啦！", url, "Error");
                    return;
                }
                JscriptPrint("修改信息成功啦！", url, "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", url, "Error");
                    return;
                }
                JscriptPrint("添加信息成功啦！", url, "Success");
            }
        }
    }
}